package com.ruoyi.web.controller.module.material;

import java.util.Arrays;

import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.MaterialGroup;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.module.IMaterialGroupService;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 素材分组Controller
 *
 * @author ruoyi
 * @date 2021-08-13
 */
@Validated
@Api(value = "素材分组控制器", tags = {"素材分组管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/material/group")
public class MaterialGroupController extends BaseController {

    private final IMaterialGroupService materialGroupService;

    /**
     * 查询素材分组列表
     */
    @ApiOperation("查询素材分组列表")
    @GetMapping("/list")
    public TableDataInfo<MaterialGroup> list(@Validated MaterialGroup materialGroup) {
        return PageUtils.buildDataInfo(materialGroupService.page(PageUtils.buildPage()));
    }


    /**
     * 查询素材分组列表
     */
    @ApiOperation("查询素材分组列表")
    @GetMapping("/listNoPage")
    public AjaxResult listNoPage(@Validated MaterialGroup materialGroup) {
        return AjaxResult.success(materialGroupService.list());
    }

    /**
     * 获取素材分组详细信息
     */
    @ApiOperation("获取素材分组详细信息")
    @GetMapping("/{id}")
    public AjaxResult<MaterialGroup> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") String id) {
        return AjaxResult.success(materialGroupService.getById(id));
    }

    /**
     * 新增素材分组
     */
    @ApiOperation("新增素材分组")
    @Log(title = "素材分组", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody MaterialGroup materialGroup) {
        return toAjax(materialGroupService.save(materialGroup) ? 1 : 0);
    }

    /**
     * 修改素材分组
     */
    @ApiOperation("修改素材分组")
    @Log(title = "素材分组", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody MaterialGroup materialGroup) {
        return toAjax(materialGroupService.updateById(materialGroup) ? 1 : 0);
    }

    /**
     * 删除素材分组
     */
    @ApiOperation("删除素材分组")
    @Log(title = "素材分组" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")@PathVariable String[] ids) {
        return toAjax(materialGroupService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
